<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
include "../DBManager.php";

// -----------------------------------------------------------------------------
// 访问设置
// -----------------------------------------------------------------------------

if (!(isset($_GET["class"]) && isset($_GET["status"]) && isset($_GET["sort"]) && isset($_GET["sequence"]))) {
    exit("需要传入有效的参数！");
}

// -------------------------------------------------------------------------
// 数据库相关设置
// -------------------------------------------------------------------------

$dbPath = "../" . $dbPath; // 不加这行语句的话会报错，提示找不到对应路径
$db = new DBManager("sqlite:" . $dbPath);

// -------------------------------------------------------------------------

$class = $_GET["class"];
$status = $_GET["status"];
$sort = $_GET["sort"];
$sequence = $_GET["sequence"];

$goals = $db->getListOfGoals($class, $status, $sort, $sequence);

// -------------------------------------------------------------------------

if (!is_array($goals)){

    echo "获取数据失败。";

} else {

    if (empty($goals)) {

        echo "暂无。";

    } else {

        foreach ($goals as $goal) {

            $id = $goal["id"];
            $description = $goal["description"];

            if (mb_strlen($description) > 38) {
                $description = mb_substr($description, 0, 36, "utf-8") . "……";
            }

            $sortNumber = $goal["sortNumber"];
            $createTime = $goal["createTime"];
            $createTime = date("Ymd", $createTime);
            $lastEditTime = $goal["lastEditTime"];
            $lastEditTime = date("Ymd", $lastEditTime);
            $achieveDate = $goal["achieveDate"];
            $achieveDate = date("Ymd", strtotime($achieveDate));
            $giveupDate = $goal["giveupDate"];
            $giveupDate = date("Ymd", strtotime($giveupDate));
            $startDate = $goal["startDate"];
            $startDate = date("Ymd", strtotime($startDate));
            $endDate = $goal["endDate"];
            $endDate = date("Ymd", strtotime($endDate));

            $sortInforHtml = "";

            switch($sort) {

                case "sortNumber":

                    $sortInforHtml = <<<BBB
                        <div class="btn-group" role="group">
                            <!-- 注意自定义属性是不区分大小写的 -->
                            <button type="button" 
                                    data-id="{$id}"
                                    data-sortnumber="{$sortNumber}"
                                    class="btn btn-outline-secondary btnUp">↑</button>
                            <button type="button" 
                                    data-id="{$id}"
                                    data-sortnumber="{$sortNumber}"
                                    class="btn btn-outline-secondary btnDown">↓</button>
                        </div>
                    BBB;

                    break;

                case "createTime":

                    $sortInforHtml = <<<BBB
                        <small class="text-muted">创建于 $createTime</small>
                    BBB;

                    break;

                case "lastEditTime":

                    $sortInforHtml = <<<BBB
                        <small class="text-muted">编辑于 $lastEditTime</small>
                    BBB;

                    break;

                case "achieveDate":

                    $sortInforHtml = <<<BBB
                        <small class="text-muted">完成于 $achieveDate</small>
                    BBB;

                    break;

                case "giveupDate":

                    $sortInforHtml = <<<BBB
                        <small class="text-muted">放弃于 $giveupDate</small >
                    BBB;

                    break;

                case "startDate":

                    $sortInforHtml = <<<BBB
                        <small class="text-muted">$startDate 开始</small>
                    BBB;

                    break;

                case "endDate":

                    $sortInforHtml = <<<BBB
                        <small class="text-muted">$endDate 结束</small>
                    BBB;

                    break;

            }

            echo <<<BBB
                <div class="mt-2 card shadow-sm">
                    <div class="card-body">

                        <p class="card-text">$description</p>
                        <div class="d-flex justify-content-between align-items-center">

                            <div class="btn-group" role="group">
                                <a href="view.php?id={$id}" class="btn btn-outline-secondary">查看</a>
                                <a href="edit.php?id={$id}" class="btn btn-outline-secondary">编辑</a>
                                <button type="button"
                                        data-id="{$id}"
                                        class="btn btn-outline-secondary btnDelete">删除</button>
                            </div>

                            $sortInforHtml

                        </div>

                    </div>

                </div>
            BBB;

        }

    }

}
